package handlers

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"week1/api/request"
	"week1/api/response"
	"week1/dao"
)

// 文章发布
func AddArticle(c *gin.Context) {
	var data request.ArticleRequest
	err := c.ShouldBind(&data)
	if data.Title == "" {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "标题不能为空",
			"Data": nil,
		})
		return
	}
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}
	result := dao.AddArticle(data.Title, data.Context)
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "Success",
		"Data": result,
	})
}

// 文章的浏览日志
func ArticleHistory(c *gin.Context) {
	var data request.ArticleHistoryRequest
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}
	result := dao.ArticleHistory(data.ArticleId)
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "Success",
		"Data": result,
	})
}

// 文章点赞
func ArticleLike(c *gin.Context) {
	var data request.ArticleLikeRequest
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}
	result := dao.ArticleLike(data.ArticleId)
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "Success",
		"Data": result,
	})
}

// 文章评论
func AddArticleReview(c *gin.Context) {
	var data request.AddArticleReviewRequest
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}
	result := dao.AddArticleReview(data.ArticleId, data.Review)
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "Success",
		"Data": result,
	})
}

// 文章回复评论
func ResArticleReview(c *gin.Context) {
	var data request.ResArticleReviewRequest
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}
	result := dao.ResArticleReview(data.ArticleId, data.FatherId, data.Review)
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "Success",
		"Data": result,
	})
}

// 文章评论点赞
func ReviewLike(c *gin.Context) {
	var data request.ReviewLikeRequest
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}
	result := dao.ReviewLike(data.ReviewId)
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "Success",
		"Data": result,
	})
}

func GetArticleId(c *gin.Context) {
	var data request.GetArticleIdRequest
	err := c.ShouldBind(&data)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"Code": 500,
			"Msg":  "失败",
			"Data": nil,
		})
		return
	}
	result := dao.GetArticleId(data.Id)
	result1 := response.ArticleResponse{
		Id:        result.Id,
		Title:     result.Title,
		Context:   result.Context,
		Deleted:   result.Deleted,
		CreatedAt: result.CreatedAt,
		UpdatedAt: result.UpdatedAt,
		ReviewNum: dao.GetArticleReviewNum(result.Id),
	}
	c.JSON(http.StatusOK, gin.H{
		"Code": 200,
		"Msg":  "Success",
		"Data": result1,
	})
}
